﻿using ProductMonitor.Models;
using ProductMonitor.UserControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ProductMonitor.ViewModels
{
    internal class MainWindowVM:INotifyPropertyChanged
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindowVM()
        {
            #region 初始化环境监控
            _EnviromentList.Add(new EnviromentModel { EnItemName = "光照（Lux）", EnItemValue = 123 });
            _EnviromentList.Add(new EnviromentModel { EnItemName = "噪音（db）", EnItemValue = 55 });
            _EnviromentList.Add(new EnviromentModel { EnItemName = "温度（℃）", EnItemValue = 80 });
            _EnviromentList.Add(new EnviromentModel { EnItemName = "湿度（%）", EnItemValue = 43 });
            _EnviromentList.Add(new EnviromentModel { EnItemName = "PM2.5（m³）", EnItemValue = 20 });
            _EnviromentList.Add(new EnviromentModel { EnItemName = "硫化氢（ppm）", EnItemValue =15  });
            _EnviromentList.Add(new EnviromentModel { EnItemName = "氮气（ppm）", EnItemValue = 18 });
            #endregion

            #region 初始化报警列表
            _AlarmList.Add(new AlarmModel { Num = "01", Msg = "设备温度过高", Time = "2023-11-23 18:34:56", Duration=7 });
            _AlarmList.Add(new AlarmModel { Num = "02", Msg = "车间温度过高", Time = "2023-12-08 20:40:59", Duration = 10 });
            _AlarmList.Add(new AlarmModel { Num = "03", Msg = "设备转速过快", Time = "2024-01-05 12:24:34", Duration = 12 });
            _AlarmList.Add(new AlarmModel { Num = "04", Msg = "设备气压偏低", Time = "2024-02-04 19:58:00", Duration = 90 });
            #endregion

            #region 初始化设备监控
            _DeviceList.Add(new DeviceModel { DeviceItem = "电能（Kw,h）", Value = 60.8d });
            _DeviceList.Add(new DeviceModel { DeviceItem = "电压（V）", Value = 390d });
            _DeviceList.Add(new DeviceModel { DeviceItem = "电流（A）", Value = 5d });
            _DeviceList.Add(new DeviceModel { DeviceItem = "压差（kpm）", Value =13d });
            _DeviceList.Add(new DeviceModel { DeviceItem = "温度（℃）", Value = 36d });
            _DeviceList.Add(new DeviceModel { DeviceItem = "振动（mm/s）", Value = 4.1d });
            _DeviceList.Add(new DeviceModel { DeviceItem = "转速（r/min）", Value = 2600d });
            _DeviceList.Add(new DeviceModel { DeviceItem = "气压（kpm）", Value = 0.5d });
            #endregion

            #region 初始化雷达数据
            _RaderList.Add(new RaderModel { ItemName = "排烟风机", ItemValue = 90d });
            _RaderList.Add(new RaderModel { ItemName = "客梯", ItemValue = 30.00d });
            _RaderList.Add(new RaderModel { ItemName = "供水机", ItemValue = 34.89d });
            _RaderList.Add(new RaderModel { ItemName = "喷淋水泵", ItemValue = 69.59d });
            _RaderList.Add(new RaderModel { ItemName = "稳压设备", ItemValue = 20d });
            #endregion

            #region 初始化缺岗数据
            _StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "张晓婷", Position = "技术员", OutWorkHour = 123 });
            _StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "李晓", Position = "操作员", OutWorkHour = 23 });
            _StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "王科健", Position = "技术员", OutWorkHour = 134 });
            _StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "陈家东", Position = "统计员", OutWorkHour = 143 });
            _StuffOutWorkList.Add(new StuffOutWorkModel { StuffName = "杨过", Position = "技术员", OutWorkHour = 12 });
            #endregion

            #region 初始化车间数据
            _WorkShopList.Add(new WorkShopModel { WorkShopName = "贴片车间", WorkingCount = 32, WaitingCount = 8, WrongCount = 4, StopCount = 0 });
            _WorkShopList.Add(new WorkShopModel { WorkShopName = "封装车间", WorkingCount = 20, WaitingCount = 8, WrongCount = 4, StopCount = 0 });
            _WorkShopList.Add(new WorkShopModel { WorkShopName = "焊接车间", WorkingCount = 32, WaitingCount = 10, WrongCount = 4, StopCount = 10 });
            _WorkShopList.Add(new WorkShopModel { WorkShopName = "贴片车间", WorkingCount = 68, WaitingCount = 8, WrongCount = 4, StopCount = 0 });
            #endregion

            #region 初始化机台列表

            Random random = new();
            for (int i = 0; i < 20; i++)
            {
                int plan = random.Next(100, 1000);
                int finished = random.Next(0, plan);
                MachineList.Add(new MachineModel
                {
                    MachineName = "焊接机"+(i+1),
                    FinishedCount = finished,
                    PlanCount = plan,
                    Status = "作业中",
                    OrderNum = "H202212345678"
                });
            }

            #endregion
        }


        private UserControl? _MonitorUC;

		public UserControl MonitorUC
        {
            get
            {
                _MonitorUC ??= new MonitorUC();
				return _MonitorUC; 
			}
			set { 
				_MonitorUC = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MonitorUC)));
            }
		}

        public event PropertyChangedEventHandler? PropertyChanged;

        #region 时间
        /// <summary>
        /// 时间 小时 秒
        /// </summary>
        public string TimeStr
        {
            get { return DateTime.Now.ToString("HH:mm"); }
        }

        /// <summary>
        /// 时间 年月日
        /// </summary>
        public string DateStr
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd"); }
        }

        /// <summary>
        /// 时间 星期几
        /// </summary>
        public string WeekStr
        {
            get { 
                int index = (int) DateTime.Now.DayOfWeek;
                return index switch
                {
                    0 => "星期天",
                    1 => "星期一",
                    2 => "星期二",
                    3 => "星期三",
                    4 => "星期四",
                    5 => "星期五",
                    6 => "星期六",
                    _ => "未知日期",
                };
            }
        }
        #endregion

        #region 计数
        /// <summary>
        /// 机台总数
        /// </summary>
        private string _MachineCount="0298";

        public string MachineCount
        {
            get { return _MachineCount; }
            set { _MachineCount = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MachineCount)));
            }
        }


        /// <summary>
        /// 生产计数
        /// </summary>
        private string _ProductCount = "1643";

        public string ProductCount
        {
            get { return _ProductCount; }
            set
            {
                _ProductCount = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ProductCount)));
            }
        }

        /// <summary>
        /// 不良计数
        /// </summary>
        private string _BadCount = "0403";

        public string BadCount
        {
            get { return _BadCount; }
            set
            {
                _BadCount = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BadCount)));
            }
        }

        #endregion

        #region 环境监控数据
        /// <summary>
        /// 环境监控数据
        /// </summary>
        private List<EnviromentModel> _EnviromentList = new();

        public List<EnviromentModel> EnviromentList
        {
            get { return _EnviromentList; }
            set {
                _EnviromentList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EnviromentList)));
            }
        }

        #endregion

        #region 报警属性

        /// <summary>
        /// 报警集合
        /// </summary>
        private List<AlarmModel> _AlarmList = new();

        public List<AlarmModel> AlarmList
        {
            get { return _AlarmList; }
            set { 
                _AlarmList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AlarmList)));
            }
        }

        #endregion

        #region 数据监控项
        private List<DeviceModel> _DeviceList = new();

        public List<DeviceModel> DeviceList
        {
            get { return _DeviceList; }
            set {
                _DeviceList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DeviceList)));
            }
        }

        #endregion

        #region 雷达数据属性
        private List<RaderModel> _RaderList = new();

        public List<RaderModel> RaderList
        {
            get { return _RaderList; }
            set { 
                _RaderList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(RaderList)));
            }
        }

        #endregion

        #region 缺岗数据属性
        private List<StuffOutWorkModel> _StuffOutWorkList = new();

        public List<StuffOutWorkModel> StuffOutWorkList
        {
            get { return _StuffOutWorkList; }
            set {
                _StuffOutWorkList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StuffOutWorkList)));
            }
        }

        #endregion

        #region 车间数据属性

        private List<WorkShopModel> _WorkShopList = new();

        public List<WorkShopModel> WorkShopList
        {
            get { return _WorkShopList; }
            set { 
                _WorkShopList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(WorkShopList)));
            }
        }

        #endregion

        #region 机床数据属性
        private List<MachineModel> _MachineList=new();

        public List<MachineModel> MachineList
        {
            get { return _MachineList; }
            set
            {
                _MachineList = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MachineList)));
            }
        }
        #endregion
    }
}
